package net.njcp.mybatis.frame.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: mybatis-frame
 * @description: 正则判断
 * @author: 柳拓
 * @create: 2019-12-18 11:00
 */
public class MatchesUtil {

    /**
     * 使用正则表达式来判断字符串中是否包含字母
     * @param name 待检验的字符串
     * @return 返回是否包含
     * true: 包含字母 ;false 不包含字母
     */
    public static boolean judgeContainsStr(String name) {
        String regex=".*[a-zA-Z]+.*";
        Matcher m=Pattern.compile(regex).matcher(name);
        return m.matches();
    }

    public static String switchParam(String name) {
        String tableName = ReflectUtil.toLowerCaseFirstOne(name);
        if (name.matches(".*[A-Z]+.*")){
            Pattern pattern = Pattern.compile("[A-Z]");
            Matcher matcher = pattern.matcher(name);
            while(matcher.find()){
                String old = matcher.group();
                String ne = matcher.group().toLowerCase();
                tableName = tableName.replaceAll(old, "_"+ne);
            }
        }
        return tableName;
    }

    //判断是否为类名或者属性名
    public static boolean isClassOrPro(String name){
        //判断是否为驼峰标识
        if (name.matches(".*[A-Z][a-z]+.*") || name.matches(".*[A-Z][A-Z][a-z]+.*")){
            //判断是否包含特殊字符
            if (isContainsSpecialChar(name)){
                return false;
            }
            return true;
        }
        return false;
    }

    public static boolean isContainsSpecialChar(String name){
        String regEx="[`~!@#$%^&*_]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(name);
        return m.find();
    }


    public static void main(String[] args) {
        isClassOrPro("protocolId");

        System.out.println(isContainsSpecialChar("protocol_Id"));
    }

}
